package com.ica.model.contact;

import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Vector;

import android.util.Log;

import com.ica.model.contact.util.ContactUtils;
import com.ica.model.util.IdentifiableModelObject;
import com.ica.model.policy.Policy;

/**
 * @author   Hector
 */
public class ContactGroup extends IdentifiableModelObject {
	
	/**
	 */
	private String name = null;
	/** 
	 * @uml.property name="contacts"
	 * @uml.associationEnd multiplicity="(0 -1)" aggregation="composite" inverse="contactGroups:com.ica.model.contact.Contact"
	 */
	private HashMap<Long, Contact> contacts;
	
	public ContactGroup(String name) {
		this.name = name;
		contacts = new HashMap<Long,Contact>();
	}
	
	/**
	 * @return
	 * @uml.property  name="name"
	 */
	public String getName() {
		return name;
	}

	/**
	 * @param  name
	 * @uml.property  name="name"
	 */
	public void setName(String name) {
		this.name = name;
	}
	
	public void addContact(Collection<Contact> newContacts) {
		for(Contact contact : newContacts)
			addContact(contact);
	}
	
	public void addContact(Contact contact) {
		contact.addContactGroup(this);
		contacts.put(contact.getId(),contact);
	}
	
	public boolean removeContact(Contact contact) {
		if (contact==null) return false;
		contact.removeContactGroup(this);
		return contacts.remove(contact.getId()) != null;
	}
	
	private Contact getContactById(long id, List<Contact> list) {
		for(Contact contact : list)
			if (contact.getId()==id) return contact;
		return null;
	}
	
	public void replaceContacts(List<Contact> selectedContacts) {
		
		for(Contact contact : selectedContacts) {
			Contact c = contacts.get(contact.getId());
			// New Contact
			if (c==null) addContact(contact);
		}
		List<Contact> currentContacts = new Vector<Contact>(contacts.values());
		for(Contact contact : currentContacts) {
			Contact c = getContactById(contact.getId(),selectedContacts);
			// Contact that no longer belongs to the group
			if (c==null) removeContact(contact);
		}
	}
	
	/** 
	 * @return
	 * @uml.property  name="contacts"
	 */
	public Iterator<Contact> getContacts() {
		return contacts.values().iterator();
	}

	public boolean contains(String phoneNumber) {	
		for (Contact c : contacts.values()) {
			Log.w("DEBUG","Contact Group ["+name+"] -> ["+c.getPhoneNumber()+"] == ["+phoneNumber+"] ?");
			if (ContactUtils.comparePhoneNumbers(c.getPhoneNumber(), phoneNumber)) return true;
		}
		return false;
	}
	
	@Override
	public String toString() {
		return name;
	}

	/** 
	 * @uml.property name="policy"
	 * @uml.associationEnd multiplicity="(0 -1)" inverse="contactGroup:com.ica.model.policy.Policy"
	 */
	private Collection<Policy> policy;

	/** 
	 * Getter of the property <tt>policy</tt>
	 * @return  Returns the policy.
	 * @uml.property  name="policy"
	 */
	public Collection<Policy> getPolicy() {
		return policy;
	}

	/** 
	 * Setter of the property <tt>contacts</tt>
	 * @param contacts  The contacts to set.
	 * @uml.property  name="contacts"
	 */
	public void setContacts(HashMap<Long, Contact> contacts) {
		this.contacts = contacts;
	}

	/** 
	 * Setter of the property <tt>policy</tt>
	 * @param policy  The policy to set.
	 * @uml.property  name="policy"
	 */
	public void setPolicy(Collection<Policy> policy) {
		this.policy = policy;
	}
}
